import getTitle from './getTitle'
import http from '../utils/HTTP'
import api from '../utils/api'
import voice from '../utils/voice'
import Toast from '@vant/weapp/toast/toast'
let tableidList = ['605c4767a838e6708442e704', '605c5424cc596258042d5438', '6063528ac7878851bc64c70b']
let tableList = ['船政衙门', '罗星塔公园', '阳光学院']
let table_id = ''
let poiLocation = '' //poi点坐标
let park = false

function judument(lat, lng) {
  for (const key in tableList) {
    if (getTitle.getTitle(lat, lng) == tableList[key]) {
      table_id = tableidList[key]
    }
  }
  poiLocation = `${lat},${lng}` //下面的location
  console.log(poiLocation); //当前定位点
}

function search(qqmapsdk) {
  let attractionsList = []
  qqmapsdk.reverseGeocoder({
    success: (lo) => {
      const {
        lat,
        lng
      } = lo.result.location;
      console.log(lo.result.location);
      // 判定是否在当前范围内。若是，则更改相应的table_id
      // 测试25.981835,119.457283 罗星塔
      //26.004261,119.449420 阳光学院
      judument(lat, lng)
      // 如果当前位置在罗星塔，则请求第二页，并且合并两页数据
      let parkList = []
      let parkListArr = [{}]
      if (getTitle.getTitle(lat, lng) == '罗星塔公园') {
        park = true
        //请求第二页数据
        let parkData = {
          key: 'NMKBZ-SACK6-WFDSR-M7UUJ-Q7OYF-FQB6V',
          keyword: '福州', //关键词
          radius: '2000', //2k米
          location: poiLocation,
          auto_extend: 1, //若搜不到则扩大范围		
          page_index: 2, //页码
          table_id: table_id,
        }
        http.get(api.poiurl, parkData, res => {
          console.log('罗星塔第二页,请求成功', res);
          for (let i = 0; i < res.data.result.data.length; i++) {
            //第二页的marker处理。
            parkList.push(
              ...parkListArr.map(
                ({
                  id,
                  longitude,
                  latitude,
                  callout,
                  width,
                  height,
                  iconPath,
                  label
                }) => ({
                  id: i,
                  longitude: res.data.result.data[i].location.lng,
                  latitude: res.data.result.data[i].location.lat,
                  callout: {
                    content: '查看景点介绍',
                    color: '#1296db',
                    fontSize: 12,
                    borderRadius: 4,
                    borderWidth: 1,
                    borderColor: '#c23333',
                    bgColor: '#ffffff',
                    padding: 6,
                    display: 'BYCLICK', //'BYCLICK':点击显示; 'ALWAYS':常显
                  },
                  label: {
                    content: res.data.result.data[i].title,
                    color: '#F24E4E',
                    fontSize: 12,
                    borderRadius: 4,
                    borderWidth: 1,
                    borderColor: '#c23333',
                    bgColor: '#ffffff90',
                    padding: 6,
                    anchorX: 20,
                    anchorY: -30,
                  },
                  width: 30,
                  height: 30,
                  iconPath: '../../images/周边.png',
                })
              )
            );
          }
        }, err => {
          console.log('错误信息,', err);
        })
      }
      let data = {
        key: 'NMKBZ-SACK6-WFDSR-M7UUJ-Q7OYF-FQB6V',
        keyword: '福州', //关键词
        radius: '2000', //2k米
        location: poiLocation,
        auto_extend: 1, //若搜不到则扩大范围			
        table_id: table_id,
      }
      http.get(api.poiurl, data, res => {
        console.log('查找周边景点，请求成功', res);
        Toast.loading({
          message: '正在搜索周边景点',
          forbidClick: true,
        })
        voice.wordYun('正在搜索周边景点')
        let markers = []
        let markersArr = [{}]
        for (let i = 0; i < res.data.result.data.length; i++) {
          //处理markers标点
          markers.push(
            ...markersArr.map(
              ({
                id,
                longitude,
                latitude,
                callout,
                width,
                height,
                iconPath,
                label
              }) => ({
                id: i,
                longitude: res.data.result.data[i].location.lng,
                latitude: res.data.result.data[i].location.lat,
                callout: {
                  content: '查看景点介绍',
                  color: '#1296db',
                  fontSize: 12,
                  borderRadius: 4,
                  borderWidth: 1,
                  borderColor: '#c23333',
                  bgColor: '#ffffff',
                  padding: 6,
                  display: 'BYCLICK', //'BYCLICK':点击显示; 'ALWAYS':常显
                },
                label: {
                  content: res.data.result.data[i].title,
                  color: '#F24E4E',
                  fontSize: 12,
                  borderRadius: 4,
                  borderWidth: 1,
                  borderColor: '#c23333',
                  bgColor: '#ffffff90',
                  padding: 6,
                  anchorX: 20,
                  anchorY: -30,
                },
                width: 30,
                height: 30,
                iconPath: '../../images/周边.png',
              })
            )
          );
        }
        if (park) {
          for (const k in parkList) {
            markers.push(parkList[k])
          }
        }
        for (const k in markers) {
          attractionsList.push(markers[k].label.content)
        }
        attractionsList.reverse()
        markers.reverse()
        this.setData({
          //点击搜索跳转到第一个景点
          longitude: res.data.result.data[0].location.lng,
          latitude: res.data.result.data[0].location.lat,
          markers: markers,
          list: attractionsList,
          pointimg: '', //显示周围景点的导航按钮
          areaimg: 'none' //隐藏所有景区的导航按钮
        })
      }, err => {
        console.log('错误信息,', err);
        voice.wordYun('不在船政旅游景区内')
        Toast.fail('不在船政旅游景区内')
        this.setData({
          active: 0
        })
      })
    }
  })
}
export default {
  search
}